// pages/partner/classroom/classroom.js
var shihan = require('../../../common/fetch')
var config = require('../../../common/config')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    page: 1,
    page_size: 20,
    currentTab: 0,
    listflag: true,
    has_more: true,
    refreshflag: false,
    all_class_list: [],
    showLoading: true,
    res_navbar: ['全部', '新手入门', '培训进阶', '优秀案例']
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      currentTab: options.index || 0
    })
    this.refreshClassList(this.data.currentTab)
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  onResNavbarTap: function (e) {
    this.setData({
      page: 1,
      page_size: 20,
      all_class_list: [],
      showLoading: true,
      currentTab: e.currentTarget.dataset.index
    })
    this.refreshClassList(e.currentTarget.dataset.index)
  },

  refreshClassList: function (a) {
    var that = this
    shihan.sendRequest(config.classroom_list, 'POST', {
      type: (a-1) < 0 ? '' : a-1,
      page: that.data.page,
      page_size: that.data.page_size
    })
      .then(function (response) {
        if (response.data.code == 0 && response.data.data.sh_items.length > 0) {
          that.setData({
            page: that.data.page + 1,
            all_class_list: that.data.all_class_list.concat(response.data.data.sh_items),
            listflag: true,
            has_more: true
          })
          if (that.data.all_class_list.length == response.data.data.sh_total_rows) {
            that.setData({
              listflag: false,
              has_more: false
            })
          } 
        } else {
          that.setData({
            listflag: false,
            has_more: false
          })
        }
        that.setData({
          showLoading: false
        })
        wx.stopPullDownRefresh()
      }, function (error) {
        console.log(error);
      });
  },
  openMyclass: function (e) {
    wx.navigateTo({
      url: '/pages/strategyinfo/strategyinfo?share=0&nologin=1&url=' + encodeURIComponent(e.currentTarget.dataset.url)
    })
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.setData({
      page: 1,
      page_size: 20,
      listflag: true,
      showLoading: true,
      all_class_list: []
    })
    this.refreshClassList(this.data.currentTab)
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (this.data.listflag) {
      this.setData({
        listflag: false
      })
      this.refreshClassList(this.data.currentTab)
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  }
})